local skels = {}
---@class DIYUtility
local DIY = require "packages/diy_utility/_base"

local hongzhen = fk.CreateSkill{
    name = "ym2__hongzhen",
}
local hongzhen2 = fk.CreateSkill{
    name = "ym2__hongzhenEx",
    dynamic_desc = function (self, player, lang)
        local str = "转换技，所有角色回合内，以下时机，"
        str = DIY.getSwitchState(player,self.name) == 1 and str .. "<font color='#6fb8cc'>天：濒死结算时向你求【桃】时；</font>" or str .. "天：濒死结算时向你求【桃】时；"
        str = DIY.getSwitchState(player,self.name) == 2 and str .. "<font color='#6fb8cc'>地：对一号位或你使用牌时；</font>" or str .. "地：对一号位或你使用牌时；"
        str = DIY.getSwitchState(player,self.name) == 3 and str .. "<font color='#6fb8cc'>人：脱离濒死或受伤状态后。</font>" or str .. "人：脱离濒死或受伤状态后。"
        str = str .. "你可移动场上一张牌并横置最短路径上的所有角色。"
        if player:getMark("ym2__huiqi-zhoushi") ~= 0 then
            str = str .. "周始：你分配1点雷电伤害并摸三张牌"
        end
        return str
    end,
}
Fk:loadTranslationTable{
    ["ym2__hongzhen"] = "鸿征",
    [":ym2__hongzhen"] = "所有角色回合内，以下时机，"..
    "天：濒死结算时向你求【桃】时；"..
    "地：对一号位或你使用牌时；"..
    "人：脱离濒死或受伤状态后。"..
    "你可移动场上一张牌并横置最短路径上的所有角色。",
    ["#ym2__hongzhen-invoke"] = "是否发动 鸿征，移动场上一张牌并横置最短路径上的所有角色",
    ["#ym2__hongzhen-choose"] = "鸿征：横置一条最短路径上的所有角色",
    ["#ym2__hongzhen-left"] = "向左（%arg）",
    ["#ym2__hongzhen-right"] = "向右（%arg）",
    ["$ym2__hongzhen1"] = "鸿鹄振翼，志在千里！虽千万人，吾往矣！",
    ["$ym2__hongzhen2"] = "明知必死，死国可乎？！鸿征！讨逆！",
    ["$ym2__hongzhen3"] = "白虹贯日，孤臣一诺！此‘征’，为社稷而‘鸿’！",

    ["ym2__hongzhenEx"] = "鸿征",
    [":ym2__hongzhenEx"] = "转换技，所有角色回合内，以下时机，"..
    "天：濒死结算时向你求【桃】时；"..
    "地：对一号位或你使用牌时；"..
    "人：脱离濒死或受伤状态后。"..
    "你可移动场上一张牌并横置最短路径上的所有角色。"..
    "周始：你分配1点雷电伤害并摸三张牌",
    ["#ym2__hongzhenEx-choose"] = "鸿征：分配1点雷电伤害",
}
local spec = {
    on_cost = function (self, event, target, player, data)
        local tos = player.room:askToChooseToMoveCardInBoard(player,{
            skill_name = hongzhen.name,
            prompt = "#ym2__hongzhen-invoke"
        })
        if #tos == 2 then
            event:setCostData(self,tos)
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        if DIY.getSwitchState(player,hongzhen2.name) ~= 0 then
            player:broadcastSkillInvoke(hongzhen2.name,DIY.getSwitchState(player,hongzhen2.name))
        end
        DIY.changeSwitchState(player,hongzhen2.name,true)
        local room = player.room
        local targets = event:getCostData(self)
        room:askToMoveCardInBoard(player,{
            target_one = targets[1],
            target_two = targets[2],
            skill_name = hongzhen.name,
        })
        local choices = {}
        local from, to = targets[1], targets[2]
        local ret, left, right = {}, {from}, {from}
        local temp = from.next
        while temp ~= to do
            if temp:isAlive() then
                table.insertIfNeed(right, temp)
            end
            temp = temp.next
        end
        temp = to.next
        while temp ~= from do
            if temp:isAlive() then
                table.insertIfNeed(left, temp)
            end
            temp = temp.next
        end
        table.insertIfNeed(left,to)
        table.insertIfNeed(right,to)
        local min_dist = math.min(#left, #right)
        if #left == min_dist then
            table.insert(choices,"#ym2__hongzhen-left:::"..table.concat(table.map(left, function (tp)
                return Fk:translate(tp.general)
            end),","))
        end
        if #right == min_dist and left ~= right then
            table.insert(choices,"#ym2__hongzhen-right:::"..table.concat(table.map(right, function (tp)
                return Fk:translate(tp.general)
            end),","))
        end
        local choice = room:askToChoice(player,{
            choices = choices,
            skill_name = hongzhen.name,
            prompt = "#ym2__hongzhen-choose",
        })
        if choice:startsWith("#ym2__hongzhen-left") then
            table.insertTableIfNeed(ret, left)
        else
            table.insertTableIfNeed(ret, right)
        end
        for _, tp in ipairs(ret) do
            tp:setChainState(true)
        end
        if player:getMark("ym2__huiqi-zhoushi") ~= 0 and DIY.getSwitchState(player,hongzhen2.name) == 1 then
            local tos = room:askToChoosePlayers(player,{
                targets = room.alive_players,
                max_num = 1,
                min_num = 1,
                prompt = "#ym2__hongzhenEx-choose",
                skill_name = hongzhen.name,
                cancelable = false,
            })
            if #tos > 0 then
                room:damage { from = player, to = tos[1], damage = 1, damageType = fk.ThunderDamage, skillName = hongzhen.name }
            end
            player:drawCards(3,hongzhen.name)
        end
    end,
}
hongzhen:addEffect(fk.AskForPeaches,{
    audio_index = 1,
    can_trigger = function (self, event, target, player, data)
        if target == player then
            local room = player.room
            local mark = player:getTableMark("ym2__huiqi")
            table.insertIfNeed(mark,1)
            room:setPlayerMark(player,"ym2__huiqi",mark)
            return player:hasSkill(hongzhen.name) and #room:canMoveCardInBoard() > 0
        end
    end,
    on_cost = spec.on_cost,
    on_use = spec.on_use,
})
hongzhen:addEffect(fk.CardUsing,{
    audio_index = 2,
    can_trigger = function (self, event, target, player, data)
        for _, tp in ipairs(data.tos) do
            if tp == player or tp.seat == 1 then
                local room = player.room
                local mark = player:getTableMark("ym2__huiqi")
                table.insertIfNeed(mark,2)
                room:setPlayerMark(player,"ym2__huiqi",mark)
                if player:hasSkill(hongzhen.name) and #room:canMoveCardInBoard() > 0 then
                    return true
                end
            end
        end
    end,
    on_cost = spec.on_cost,
    on_use = spec.on_use,
})
hongzhen:addEffect(fk.AfterDying,{
    audio_index = 3,
    can_trigger = function (self, event, target, player, data)
        local room = player.room
        local mark = player:getTableMark("ym2__huiqi")
        table.insertIfNeed(mark,3)
        room:setPlayerMark(player,"ym2__huiqi",mark)
        return player:hasSkill(hongzhen.name) and #room:canMoveCardInBoard() > 0
    end,
    on_cost = spec.on_cost,
    on_use = spec.on_use,
})
hongzhen:addEffect(fk.HpChanged,{
    audio_index = 3,
    can_trigger = function (self, event, target, player, data)
        if target and not target:isWounded() then
            local room = player.room
            local mark = player:getTableMark("ym2__huiqi")
            table.insertIfNeed(mark,3)
            room:setPlayerMark(player,"ym2__huiqi",mark)
            return player:hasSkill(hongzhen.name) and #room:canMoveCardInBoard() > 0
        end
    end,
    on_cost = spec.on_cost,
    on_use = spec.on_use,
})
hongzhen:addEffect(fk.MaxHpChanged,{
    audio_index = 3,
    can_trigger = function (self, event, target, player, data)
        if target and not target:isWounded() then
            local room = player.room
            local mark = player:getTableMark("ym2__huiqi")
            table.insertIfNeed(mark,3)
            room:setPlayerMark(player,"ym2__huiqi",mark)
            return player:hasSkill(hongzhen.name) and #room:canMoveCardInBoard() > 0
        end
    end,
    on_cost = spec.on_cost,
    on_use = spec.on_use,
})
table.insert(skels,hongzhen)

hongzhen2:addEffect(fk.AskForPeaches,{
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(hongzhen2.name) and target == player and #player.room:canMoveCardInBoard() > 0 and DIY.getSwitchState(player,hongzhen2.name) == 1
    end,
    on_cost = spec.on_cost,
    on_use = spec.on_use,
})
hongzhen2:addEffect(fk.CardUsing,{
    can_trigger = function (self, event, target, player, data)
        for _, tp in ipairs(data.tos) do
            if tp == player or tp.seat == 1 then
                if player:hasSkill(hongzhen2.name) and #player.room:canMoveCardInBoard() > 0 and DIY.getSwitchState(player,hongzhen2.name) == 2 then
                    return true
                end
            end
        end
    end,
    on_cost = spec.on_cost,
    on_use = spec.on_use,
})
hongzhen2:addEffect(fk.AfterDying,{
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(hongzhen2.name) and #player.room:canMoveCardInBoard() > 0 and DIY.getSwitchState(player,hongzhen2.name) == 3
    end,
    on_cost = spec.on_cost,
    on_use = spec.on_use,
})
hongzhen2:addEffect(fk.HpChanged,{
    can_trigger = function (self, event, target, player, data)
        if target and not target:isWounded() then
            return player:hasSkill(hongzhen2.name) and #player.room:canMoveCardInBoard() > 0 and DIY.getSwitchState(player,hongzhen2.name) == 3
        end
    end,
    on_cost = spec.on_cost,
    on_use = spec.on_use,
})
hongzhen2:addEffect(fk.MaxHpChanged,{
    can_trigger = function (self, event, target, player, data)
        if target and not target:isWounded() then
            return player:hasSkill(hongzhen2.name) and #player.room:canMoveCardInBoard() > 0 and DIY.getSwitchState(player,hongzhen2.name) == 3
        end
    end,
    on_cost = spec.on_cost,
    on_use = spec.on_use,
})
hongzhen2:addAcquireEffect(function (self, player, is_start)
    DIY.setSwitchState(player,hongzhen2.name,1,3)
end)
table.insert(skels,hongzhen2)

return skels